// pages/issue/issue.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
   data: {
    openId:null,
    isConsent:true,
    startingPlace:'',
    destination:'',
    passPlace:'',
    beginTime: new Date().toLocaleDateString(),
    beginDetailTime:'06:00',
    startingDetailPlace:'',
    userName:'',
    phoneNum:'',
    carType:'',
    remainingSeat:'',
    shareMark:'',
    ZcarPhoto1:false,
    carPhoto1:false,
    ZcarPhoto2:false,
    carPhoto2:false,
    ZcarPhoto3:false,
    carPhoto3:false,
    isPhotoInd:1
  },

  /**
   * 生命周期函数--监听页面加载
   */

   buttonSubmit(){
    wx:wx.showLoading({title:'正在发布'})
    let data = this.data;
    if (data.startingPlace) {
      if (data.destination.length >= 2) {
        if (data.userName) {
          if (data.phoneNum.length == 11) {
            if (data.remainingSeat) {
              if (data.isConsent) {
                wx.request({
                  url: app.globalData.serverUrl+'CarShareMessage/addDriverCarShareMessage.do',
                  data:data,
                  success(res){
                    console.log(res.data)

                    if (res.data.state == 1) {
                      wx.hideLoading();
                      wx.showToast({title: '发布成功',icon: 'none'});
                      setTimeout(function(){
                        wx.switchTab({ url: '../index/index'})
                      },1500)

                    }
                  }
                })
              }else{
                wx.showToast({title: '请选择同意拼车平台说明',icon: 'none'})
              }
            }else{
              wx.showToast({title: '填写可乘人数',icon: 'none'})
            }
          }else{
            wx.showToast({title: '填写手机号码',icon: 'none'})
          }
        }else{
          wx.showToast({title: '填写姓名',icon: 'none'})
        }
      }else{
        wx.showToast({title: '填写目的地',icon: 'none'})
      }

    }else{
      wx.showToast({title: '填写出发地',icon: 'none'})
    }

  },
  openPhoto(e){
    let self = this;
    let index = e.currentTarget.dataset.index;
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'], 
      sourceType: ['album', 'camera'], 
      success: function (res) {

        var tempFilePaths = res.tempFilePaths
        if (index == 1) {self.setData({ZcarPhoto1: tempFilePaths[0]})}
          if (index == 2) {self.setData({ZcarPhoto2: tempFilePaths[0]})}
            if (index == 3) {self.setData({ZcarPhoto3: tempFilePaths[0]})}

              wx.uploadFile({
                url: app.globalData.serverUrl+'CarShareMessage/upLoadImages.do', 
                filePath: tempFilePaths[0],
                name: 'file',
                formData:{
                  'openId': app.globalData.serverOpenInfo.openid
                },
                success: function(res){
                  var data = JSON.parse(res.data)
                  if (data.state == 1) {
                    wx.showToast({title: '上传成功',icon: 'none'});
                    if (index == 1) {
                      self.setData({carPhoto1: data.data.localFilePath})
                    }
                    if (index == 2) {
                      self.setData({carPhoto2: data.data.localFilePath})
                    }
                    if (index == 3) {
                      self.setData({carPhoto3: data.data.localFilePath})
                    }
                  }
            // console.log(data)


          }
        })

          }
        })
  },
  getGpsFun(e){
    let self = this;
    app.getAddress(function(data) {
      if (e.currentTarget.dataset.lei == 1) {self.setData({startingPlace: data.result.address_component.city})}
       if (e.currentTarget.dataset.lei == 2) {self.setData({startingDetailPlace: data.result.address})}


     })
  },
  inputChangge(e){

    let key = e.currentTarget.dataset.info

    if (key == 'startingPlace') {
      this.setData({"startingPlace" : e.detail.value})
    }
    if (key == 'destination') {
      this.setData({"destination" : e.detail.value})
    }
    if (key == 'passPlace') {
      this.setData({"passPlace" : e.detail.value})
    }
    if (key == 'beginTime') {
      this.setData({"beginTime" : e.detail.value})
    }
    if (key == 'beginDetailTime') {
      this.setData({"beginDetailTime" : e.detail.value})
    }
    if (key == 'startingDetailPlace') {
      this.setData({"startingDetailPlace" : e.detail.value})
    }
    if (key == 'userName') {
      this.setData({"userName" : e.detail.value})
    }
    if (key == 'phoneNum') {
      this.setData({"phoneNum" : e.detail.value})
    }
    if (key == 'carType') {
      this.setData({"carType" : e.detail.value})
    }
    if (key == 'remainingSeat') {
      this.setData({"remainingSeat" : e.detail.value})
    }
    if (key == 'shareMark') {
      this.setData({"shareMark" : e.detail.value})
    }
    if (key == 'isConsent') {
      this.setData({"isConsent" : !this.data.isConsent})
    }
  },
  onLoad: function (options) {
   let self = this;
   
   if (app.globalData.serverOpenInfo) {
    this.setData({
      openId : app.globalData.serverOpenInfo.openid
    })
  }


},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
   onReady: function () {

   },

  /**
   * 生命周期函数--监听页面显示
   */
   onShow: function () {
    // console.log('监听页面显示')
    let self = this;
    app.sendUserInfoPage(function(data){
      if (data) {
        console.log(data)
        self.setData({
          userName:data.userName,
          phoneNum:data.phoneNum,
          carType:data.carType

        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
   onHide: function () {

    // console.log('页面隐藏')

  },

  /**
   * 生命周期函数--监听页面卸载
   */
   onUnload: function () {
    // console.log('页面卸载')


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
   onPullDownRefresh: function () {
    console.log('下拉')


  },

  /**
   * 页面上拉触底事件的处理函数
   */
   onReachBottom: function () {
    console.log('上拉')

  },

  /**
   * 用户点击右上角分享
   */
   onShareAppMessage: function () {

   }
 })